var httpsUrl = getApp().globalData.httpsUrl;
var globalData = getApp().globalData;
var commonUtil = require("../../../utils/commonUtil.js")
Page({

  data: {
    imgUrl: globalData.imgUrl,
    count:0,
    info:[],
    list:[],
    user:[],
    comment:"",
  },
  commentSave:function(){
    var that = this;
    var params = new Object();
    let articleId = wx.getStorageSync("articleIdKey");
    let comment = this.data.comment;
    if (commonUtil.isEmpty(comment)){
      commonUtil.showModal('评论不能为空！');
      return;
    }
    params.articleId = articleId;
    params.content = comment;
    console.log("params:" + JSON.stringify(params));
    let openId = wx.getStorageSync("openidKey");
    wx.showLoading({title: '请稍等...',})
    wx.request({
      header: { "openId": openId, },
      url: httpsUrl + "/comment/",
      dataType: "json",
      method: "POST",
      data: params,
      success: function (res) {
        wx.hideLoading();
        res = res.data;
        console.log("评论提交：" + JSON.stringify(res));
        if (res.code == 0) {
          that.comment();
          wx.showToast({title: '操作成功',})
          that.setData({ comment:""})
        }
      }
    })
  },
  del: function (e) {
    let id = e.currentTarget.id;
    var that = this;
    var params = new Object();
    params.commentId = id;
    console.log("params:" + JSON.stringify(params));
    let openId = wx.getStorageSync("openidKey");
    wx.showLoading({title: '请稍等...',})
    wx.request({
      header: { "openId": openId, },
      url: httpsUrl + "/comment/?commentId=" + id,
      // url:"http://127.0.0.1:8080/sharecar-xcx/comment/?commentId=5",
      dataType: "json",
      method: "DELETE",
      // data: params,
      success: function (res) {
        wx.hideLoading();
        res = res.data;
        console.log("评论删除：" + JSON.stringify(res));
        if (res.code == 0) {
          that.comment();
          wx.showToast({ title: '操作成功', })
        }
      }
    })
  },
  comment:function(){
    let articleId = wx.getStorageSync("articleIdKey");
    var that = this;
    var params = new Object();
    params.articleId = articleId;
    let openId = wx.getStorageSync("openidKey");
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/comment/",
      dataType: "json",
      method: "GET",
      data: params,
      success: function (res) {
        res = res.data;
        console.log("评论：" + JSON.stringify(res));
        if (res.code == 0) {
          that.setData({ list: res.data })
          that.setData({ count: res.data.length })
        }
      }
    })
  },
  user: function () {
    let that = this;
    let openid = wx.getStorageSync("openidKey");
    wx.request({
      header: { "openId": openid, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/userCenter/userInfo",
      dataType: "json",
      method: "GET",
      success: function (res) {
        res = res.data;
        console.log("userInfo:" + JSON.stringify(res));
        if (res.code == 0) {
          var user = res.data;
          that.setData({ user: user })
        }
      }
    })
  },
  onLoad: function (options) {
    let articleId = wx.getStorageSync("articleIdKey");
    var that = this;
    var params = new Object();
    params.artileId = articleId;
    console.log("params:" + JSON.stringify(params));
    let openId = wx.getStorageSync("openidKey");
    wx.showLoading({ title: '请稍等...', })
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/community/getArticleInfo",
      dataType: "json",
      method: "GET",
      data: params,
      success: function (res) {
        wx.hideLoading();
        res = res.data;
        console.log("article：" + JSON.stringify(res));
        if (res.code == 0) {
          that.setData({ info: res.data})
        }
      }
    })
    this.comment();
    this.user();
  },
  commentCg:function(e){
    let val = e.detail.value;
    this.setData({ comment:val})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})